package it.tcm.TeacherClassMate.util;

import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;




public class Comboine2page {
   public static void main(String[] args)throws Exception {
        System.out.println("Combines two page");        
            PdfReader reader = new PdfReader("C:\\Tmp\\equa1.pdf");
            int n = reader.getNumberOfPages();
            Rectangle psize = reader.getPageSize(1);
            float width = psize.getHeight();
            float height = psize.getWidth();      
            Document document = new Document(new Rectangle(width, height));
            PdfWriter Pdfwriter = PdfWriter.getInstance(document, new FileOutputStream("C:\\Tmp\\equa2.pdf"));
            document.open();
            PdfContentByte cb = Pdfwriter.getDirectContent();
            int i = 0;
            int p = 0;
            while (i < n) {
                document.newPage();
                p++;
                i++;
                PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i);
                cb. addTemplate(  page1, 1.0f, 0, 0, 1.0f, 150, -200);
                PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i);
                cb.addTemplate(  page2, 1.0f, 0, 0, 1.0f, 150, -300);
                PdfImportedPage page3 = Pdfwriter.getImportedPage(reader, i);
                cb.addTemplate(  page3, 1.0f, 0, 0, 1.0f, 150, -600);
                PdfImportedPage page4 = Pdfwriter.getImportedPage(reader, i);
                cb.addTemplate(  page4, 1.0f, 0, 0, 1.0f, 150, -900);
                PdfImportedPage page5 = Pdfwriter.getImportedPage(reader, i);
                cb.addTemplate(  page5, 1.0f, 0, 0, 1.0f, 150, -1600);
                PdfImportedPage page6 = Pdfwriter.getImportedPage(reader, i);
                cb.addTemplate(  page6, 1.0f, 0, 0, 1.0f, 150, -3000);
                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
                cb.beginText();
                cb.setFontAndSize(bf, 19);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0? 1 : 0)), width / 2, 40, 0);
                cb.endText();
            }
               document.close();      
       
    }
}
